51. N 皇后
为保证权益,题目请参考 51. N 皇后(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
// class Solution
// {
// public:
// char chars[10][10];
// bool could_use[10][10];
// int end;
// vector<vector<string>> res;
// vector<vector<string>> solveNQueens(int n)
// {
// this->end = n;
// for (int i = 0; i < n; ++i)
// {
// fill(chars[i], chars[i] + n, '.');
// fill(could_use[i], could_use[i] + n, true);
// }
// dfs(0);
// return res;
// }
// void dfs(int dim)
// {
// if (dim == this->end)
// {
// vector<string> tmp;
// for (int i = 0; i < this->end; ++i)
// {
// string tmp2 = "";
// for (int j = 0; j < this->end; ++j)
// {
// tmp2 = tmp2 + this->chars[i][j];
// }
// tmp.push_back(tmp2);
// }
// this->res.push_back(tmp);
// }
// else
// {
// for (int i = 0; i < this->end; ++i)
// {
// if (this->could_use[dim][i])
// {
// this->could_use[dim][i] = false;
// this->chars[dim][i] = 'Q';
// for (int j = dim + 1; j < this->end; ++j)
// {
// this->could_use[j][i] = false;
// if ((i - (j - dim) >= 0))
// {
// this->could_use[j][i - (j - dim)] = false;
// }
// if ((i + (j - dim) < this->end))
// {
// this->could_use[j][i + (j - dim)] = false;
// }
// }
// dfs(dim + 1);
// this->could_use[dim][i] = true;
// this->chars[dim][i] = '.';
// for (int j = dim + 1; j < this->end; ++j)
// {
// this->could_use[j][i] = true;
// if ((i - (j - dim) >= 0))
// {
// this->could_use[j][i - (j - dim)] = true;
// }
// if ((i + (j - dim) < this->end))
// {
// this->could_use[j][i + (j - dim)] = true;
// }
// }
// }
// }
// }
// }
// };
class Solution
{
public:
vector<vector<char>> chars;
vector<vector<char>> could_use;
int end;
vector<vector<string>> res;
vector<vector<string>> solveNQueens(int n)
{
this->end = n;
for (int i = 0; i < n; ++i)
{
vector<char> chars2;
vector<char> could_use2;
for (int j = 0; j < n; ++j)
{
chars2.push_back('.');
could_use2.push_back(true);
}
chars.push_back(chars2);
could_use.push_back(could_use2);
}
dfs(0);
return res;
}
void dfs(int dim)
{
if (dim == this->end)
{
vector<string> tmp;
for (int i = 0; i < this->end; ++i)
{
string tmp2 = "";
for (int j = 0; j < this->end; ++j)
{
tmp2 = tmp2 + this->chars[i][j];
}
tmp.push_back(tmp2);
}
this->res.push_back(tmp);
}
else
{
for (int i = 0; i < this->end; ++i)
{
if (this->could_use[dim][i])
{
this->could_use[dim][i] = false;
this->chars[dim][i] = 'Q';
for (int j = dim + 1; j < this->end; ++j)
{
this->could_use[j][i] = false;
if ((i - (j - dim) >= 0))
{
this->could_use[j][i - (j - dim)] = false;
}
if ((i + (j - dim) < this->end))
{
this->could_use[j][i + (j - dim)] = false;
}
}
dfs(dim + 1);
this->could_use[dim][i] = true;
this->chars[dim][i] = '.';
for (int j = dim + 1; j < this->end; ++j)
{
this->could_use[j][i] = true;
if ((i - (j - dim) >= 0))
{
this->could_use[j][i - (j - dim)] = true;
}
if ((i + (j - dim) < this->end))
{
this->could_use[j][i + (j - dim)] = true;
}
}
}
}
}
}
};
int main()
{
Solution so;
so.solveNQueens(4);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
Python
python
from typing import List
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
self.n = n
self.cols = set()
self.diag = set()
self.bdiag = set()
self.queens = [-1] * n
self.ans: List[List[str]] = []
self.dfs(0)
return self.ans
def dfs(self, row_idx: int):
if row_idx == self.n:
ans = []
tmp = ["."] * self.n
for q in self.queens:
tmp[q] = "Q"
ans.append("".join(tmp))
tmp[q] = "."
self.ans.append(ans)
else:
for col_idx in range(self.n):
if col_idx in self.cols:
continue
diag_idx = row_idx - col_idx + (self.n - 1)
if diag_idx in self.diag:
continue
bdiag_idx = row_idx + col_idx
if bdiag_idx in self.bdiag:
continue
self.cols.add(col_idx)
self.diag.add(diag_idx)
self.bdiag.add(bdiag_idx)
self.queens[row_idx] = col_idx
self.dfs(row_idx + 1)
self.cols.remove(col_idx)
self.diag.remove(diag_idx)
self.bdiag.remove(bdiag_idx)
self.queens[row_idx] = -1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48